<!--
 * @Author: zheng yong tao
 * @Date: 2022-01-17 20:19:21
 * @LastEditors: zheng yong tao
 * @LastEditTime: 2022-02-02 02:20:10
 * @Description: 
-->
<template>
  <div class="title-list-menu" v-if="titleList.length > 0">
    <div class="title-item">
      <div class="title-item-tip">目录</div>
      <div
        v-for="(item, index) in titleList"
        :key="index"
        class="title-item-text"
        :title="item.description"
        @click="hTagClick(index)"
      >
        {{ index + 1 + "、" + item.title }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "titleList",
  props: {
    titleList: {
      type: Array,
      default: () => []
    },
    float: {
      type: String,
      default: "right"
    },
    titleTagName: {
      type: String,
      default: "title"
    }
  },
  data() {
    return {
      tagYList: []
    };
  },
  mounted() {
    setTimeout(() => {
      this.getTagYList();
    }, 1000);
  },
  methods: {
    getTagYList() {
      let dom = document.getElementsByClassName(this.titleTagName);
      for (let i = 0; i < dom.length; i++) {
        this.tagYList.push(dom[i].offsetTop);
      }
    },
    hTagClick(index) {
      window.scrollTo(0, this.tagYList[index] - 100);
    }
  }
};
</script>

<style lang="scss" scoped>
.title-list-menu {
  background-color: white;
  max-height: calc(100vh - 120px);
  position: fixed;
  right: 1rem;
  top: 80px;
  padding: 1rem;
  width: 20vw;
  overflow-y: scroll;
  .title-item {
    text-align: left;
    line-height: 1.5rem;
    cursor: pointer;
    .title-item-tip {
      text-align: center;
      font-size: large;
      font-weight: bold;
    }
    .title-item-text {
      line-height: 2rem;
      font-weight: bolder;
    }
    .title-item-text:hover {
      color: orange;
    }
  }
}
</style>
